﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqAndLambda.Modules
{
    public class Linq方法之_Where_Count
    {
        public static void mian()
        {
            var data1 = GetProducts().Where(m =>
            {
                int a = 5;
                return m.Storage > 100;
            }).Select(m => m.ProductName);

            //总数
            var count = GetProducts().Count(m => m.Storage > 100);

            //投影
            var data3 = GetProducts().Select(m => new { m.ProductName, m.ProductId });

            //OrderBy 排列
            data3 = GetProducts().OrderByDescending(m => m.ProductId)
                .Select(m => new { m.ProductName, m.ProductId });

            //First() 获取满足条件的第一条记录            //Last()获取满足条件的最后第一条记录
            var data4 = GetProducts().First(m => m.ProductId == 1);
            data4 = GetProducts().FirstOrDefault(m => m.ProductId == 10);

            //Max:最大元素  Min最小   Sum之和  Average平均值
            var num = GetProducts().Max(m => m.Storage);


            foreach (var item in data1)
            {
                Console.WriteLine($"商品名称: {item}");
            }

            Console.WriteLine("-------------------------------------");
            Console.WriteLine($"商品数量:{count}");

            Console.WriteLine("-------------------------------------");
            foreach (var item in data3)
            {
                Console.WriteLine($"商品名称: {item.ProductName},商品Id:{item.ProductId}");
            }

            Console.WriteLine("-------------------------------------");
            Console.WriteLine($"商品Id为1的是 :{data4?.ProductName}");

            Console.WriteLine("-------------------------------------");
            Console.WriteLine($"最大库存的商品是 :{num}");


        }


        public static List<Category> GetCategories()
        {
            List<Category> list = new List<Category>(){
            new Category(){ CategoryId=1, CategoryName="服装" },
            new Category(){ CategoryId=2, CategoryName="食品" },
            new Category(){ CategoryId=3, CategoryName="办公" },
            new Category(){ CategoryId=4, CategoryName="饮料" }
        };
            return list;
        }

        public static List<Product> GetProducts()
        {
            List<Product> list = new List<Product>(){
            new Product(){ ProductId=1,  ProductName="雪中飞羽绒服" , Price=998, Storage=1000, CategoryId=1},
            new Product(){ ProductId=2, ProductName="安踏鞋子" , Price=198, Storage=288, CategoryId=1},
            new Product(){ ProductId=3, ProductName="短袖", Price=95, Storage=88, CategoryId=1 },
            new Product(){ ProductId=4, ProductName="旺旺雪饼" ,Price=16.5, Storage=18, CategoryId=2  },
            new Product(){ ProductId=5, ProductName="汇源果汁" ,Price=6, Storage=1500, CategoryId=2 },
            new Product(){ ProductId=6, ProductName="英雄钢笔"  ,Price=12.5, Storage=59, CategoryId=3 },
            new Product(){ ProductId=7, ProductName="小米音响"  ,Price=99, Storage=10, CategoryId=7 }
        };
            return list;
        }
    }
}
